package com.subang.function.controller;

import cn.hutool.json.JSONObject;
import com.subang.common.annotation.Log;
import com.subang.common.core.controller.BaseController;
import com.subang.common.core.domain.AjaxResult;
import com.subang.common.core.domain.entity.SysUser;
import com.subang.common.core.page.TableDataInfo;
import com.subang.common.enums.BusinessType;
import com.subang.common.utils.SecurityUtils;
import com.subang.common.utils.StringUtils;
import com.subang.common.utils.WeChatUtil;
import com.subang.framework.web.service.UserIntegralService;
import com.subang.system.domain.SysUserPointsDetail;
import com.subang.system.service.ISysUserPointsDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;





/**
 * 用户积分Controller
 * 
 * @author subei
 * @date 2024-11-21
 */
@RestController
@RequestMapping("/system/integral")
public class SysUserIntegralController extends BaseController
{
    @Autowired
    private ISysUserPointsDetailService sysUserIntegralService;

    @Autowired
    private UserIntegralService userIntegralService;

    /**
     * 查询用户积分列表
     */
/*    @PreAuthorize("@ss.hasPermi('system:integral:list')")*/
    @GetMapping("/list")
    public TableDataInfo list(SysUserPointsDetail sysUserIntegral)
    {
        startPage();
        if (StringUtils.isNotNull(SecurityUtils.getLoginUser())) {
            SysUser user = SecurityUtils.getLoginUser().getUser();
            if (!user.isAdmin()) {
                sysUserIntegral.setUserId(user.getUserId());
            }
            List<SysUserPointsDetail> list = sysUserIntegralService.selectSysUserPointsDetailList(sysUserIntegral);
            return getDataTable(list);
        } else {
            // 返回一个包含错误信息的 TableDataInfo 对象
            TableDataInfo errorInfo = new TableDataInfo();
            errorInfo.setMsg("用户未登录，无法访问此资源。");
            errorInfo.setCode(500); // 或者其他表示错误的代码
            return errorInfo;
        }
    }


    @PostMapping("/userPointsCheck")
    public AjaxResult userPointsCheck(@RequestBody UserPointsCheckRequest request) {
        String action = request.getAction();
        return userIntegralService.updateUserIntegral(action);
    }

    public static class UserPointsCheckRequest {
        private String action;


        public String getAction() {
            return action;
        }

        public void setAction(String action) {
            this.action = action;
        }
    }



    /**
     * 获取用户积分
     */
    @PreAuthorize("@ss.hasPermi('system:integral:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getUserIntergralInfo(@PathVariable("userId") Long userId)
    {
        return success(sysUserIntegralService.selectSysUserIntegralByUserId(userId));
    }

    /**
     * 新增用户积分
     */
    @PreAuthorize("@ss.hasPermi('system:integral:add')")
    @Log(title = "用户积分", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUserPointsDetail sysUserIntegral)
    {
        return toAjax(sysUserIntegralService.insertSysUserPointsDetail(sysUserIntegral));
    }


}
